package org.flowers.unbeaten.generator.configuration;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.flowers.unbeaten.generator.constant.SystemConstant;

import java.io.IOException;

/**
 * @description 自定义Long转换器
 * @author RyanWang
 * @date 2021-08-13 14:33:00
 * @version 1.0
 */
public class CustomLongConverter extends StdSerializer<Long> {

    public CustomLongConverter() {
        super(Long.class);
    }

    @Override
    public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 超过12个数字用String格式返回，由于js的number只能表示15个数字
        if (value.toString().length() > SystemConstant.JS_NUMBER_MAX_LENGTH) {
            jsonGenerator.writeString(value.toString());
        } else {
            jsonGenerator.writeNumber(value);
        }
    }

}
